package qeb.handheldmedical.patient;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import qeb.handheldmedical.R;
import qeb.handheldmedical.adapter.*;
import qeb.handheldmedical.dialog.MultiDialog;
import qeb.handheldmedical.helper.WSHelper;
import qeb.handheldmedical.model.User;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import qeb.handheldmedical.model.InPatient;
import qeb.handheldmedical.model.Consultation;

public class OperationListQuery extends Activity {
	private SlideListView slideListView;
	private OperationListAdapter adapter;
	private TextView tv_title;
	
	//private LinearLayout ll_search_bar;
	private JSONArray oplJsonArray;
	private User usr=new User();	
	private String consultationState;	
	//private Include search_bar;
	
	private Button btn_title_back,btn_title_right;
	
	//private boolean isRunning=false;
	private MultiDialog mDialog;
	
	private static final int UPDATE=0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.query_operationlist);
		init();
		
	}
	
	@Override
	protected void onStop(){
		super.onStop();
		//isRunning=false;
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		//isRunning=true;
	}
	
	@Override
	protected void onDestroy(){
		super.onDestroy();
		//handler.removeCallbacks(myRunnable);
	}
	
	private void init()
	{
		slideListView = (SlideListView) findViewById(R.id.slideListView10);
		tv_title=(TextView)findViewById(R.id.tv_top_title);
		btn_title_back=(Button)findViewById(R.id.btn_title_back);
		btn_title_back.setOnClickListener(MyOnClickListener);

		btn_title_right=(Button)findViewById(R.id.btn_title_right);
		btn_title_right.setOnClickListener(MyOnClickListener);
		tv_title.setText("手术通知单");
		//ll_search_bar=(LinearLayout)findViewById(R.id.ll_search_bar);
		try {
			usr = (User)getIntent().getSerializableExtra("userinfo");		
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}
		getData();
		dataBind();
	}
	
	private void getData(){
		oplJsonArray = WSHelper.GetSstzd("3","","","","",usr.BQID);
	}
	/*系统主菜单点击事件（在院病人、门诊病人等）*/
	private OnClickListener MyOnClickListener=new OnClickListener(){		
		@Override
		public void onClick(View v) {
			// TODO 自动生成的方法存根
			//Bundle bundle=new Bundle();
			switch (v.getId()) {
			case R.id.btn_title_right://刷新按钮
				if(consultationState!=null){
					mDialog = new MultiDialog(OperationListQuery.this,
							MultiDialog.DialogType.WAITING);
					mDialog.setMessage("正在加载，请稍候......");
					mDialog.show();
					new Thread(){
						
						@Override
						public void run() {
							try {
								getData();
						        myHandler.sendEmptyMessage(UPDATE);
							} catch (Exception e) {
								// TODO: handle exception
								Log.v("Login", e.toString());
							}
						}
					}.start();
				}
				break;
			case R.id.btn_title_back://返回按钮
				OperationListQuery.this.finish();
				break;			
			default:
				break;		
		}
		}
	};
	
	Handler myHandler=new Handler(){
		@Override
		public void handleMessage(Message msg){
			switch(msg.what){
			case UPDATE:
				dataBind();
				//btn_search.setClickable(true);
				break;
			default:
				break;
			}
			super.handleMessage(msg);
			//isRunning=false;
			mDialog.cancel();
		}
	};
	
	private void dataBind()
	{
		if (oplJsonArray != null && oplJsonArray.length() > 0) {

			if(adapter!=null){
				adapter.notifyDataSetChanged();
			}			
			adapter = new OperationListAdapter(oplJsonArray.length(),
					OperationListQuery.this, oplJsonArray);
			
			slideListView.setAdapter(adapter);

			/*slideListView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					clt = new Consultation();
					try {
						jobj = consultationJsonArray.getJSONObject(position);
						clt.getValue(jobj);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						// e.printStackTrace();
					} catch (InstantiationException e) {
						// TODO Auto-generated catch block
						// e.printStackTrace();
					}
					if (clt.AUTOID != null && clt.AUTOID != "") {
						Intent intent = new Intent(getApplicationContext(),
								ConsultationDetail.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.putExtra("patientInfo", ip);
						startActivity(intent);
					} else {
						Toast tt = Toast.makeText(OperationListQuery.this,
								"获取手术信息失败，请重试！", Toast.LENGTH_LONG);
						tt.show();
					}
				}
			});*/
		}
	}
}